<?php
	class Report_model extends Model{
		function Report_model(){
			parent::Model();
		}
		
		function header_daftar_pejabat_struktural($print_mode=false) {
			if ($print_mode==false) {
            	$s = "<table id=\"myTable\" class=\"tbl\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" >".
	                 "<thead><tr><th>No</th><th>N a m a </th><th>NIP</th><th>NIP Baru</th>".
	                 "<th>Jabatan</th><th>Gol / Ruang </th>".
            	     "</thead><tbody>";
			} else {
            	$s = '<table id="report" align="center" border="1" cellpadding="2" cellspacing="0"><tr>'.
                     '<th>No</th><th>N a m a </th><th>NIP</th><th>NIP Baru</th>".
	                 "<th>Jabatan</th><th>Gol / Ruang </th></tr>'.
            	     "<tr class=\"col_num\"><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>";
			}
            return $s;
		}
		
		function isi_daftar_pejabat_struktural($print_mode=false, $arr_golongan_valid=array()) {
	        
			
			$this->db->select('DISTINCT(id_pegawai) id_pegawai, id_jabatan, id_eselon, id_golongan, b.nama jabatan, c.nama nama,nip, nip_baru');
			$this->db->from('pengangkatan_jabatan');
			$this->db->join('jabatan b','id_jabatan = idspg_jabatan');
			$this->db->join('pegawai c','id_pegawai = idspg_pegawai');			
			$this->db->where('tipe = 1');
			$this->db->where_in('id_eselon',implode(',',$arr_golongan_valid));
			$this->db->order_by('tmt desc,id_eselon desc');
			
            $q = $this->db->get();

			//print_r($q->result());exit;
            $s = $print_mode?"":"[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
			
            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
            	if ($print_mode==false) {
	                if ($i % 2 == 1) {
	                    $kelas = "event2";
	                } else {
	                    $kelas = "event";
	                }
            	}
            	
                
                $this->db->select('nama, pangkat');
                $this->db->where(array('idspg_golongan' => $row->id_golongan));
				$query = $this->db->get('golongan');
				if ($query->num_rows() > 0) {
					$r = $query->row();
					$pangkat = $r->nama;//.' - '.$r->pangkat;
				} else {
					$pangkat = '-';
				}
				
				
                if ($print_mode==false) {
	                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idspg_pegawai."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>";
                } else {
	                $s .= "<tr><td><div align=\"right\">$i.</div></td>";
                }
                $s .= "<td>".str_replace($order, $replace, $row->gelar_depan.' '.$row->nama.' '.$row->gelar_belakang)."</td>".
                      "<td>".$row->nip."</td>".
					  "<td>".$row->nip_baru."</td>".
                      
                	  "<td>".$row->jabatan."</td>".
                      
                      "<td>".$pangkat."</td></tr>";
                if ($print_mode==false) {
                	$s .= "'],";
                }

                $i++;
            }
            if ($print_mode==false) {
	            $s = substr($s, 0, -1);
	            $s .= '];';
            } else {
            	$s .= "</tbody></table>";
            }
            return $s;
        }

		
		function header_daftar_urut_kepangkatan($print_mode=false) {
			if ($print_mode==false) {
            	$s = "<table id=\"myTable\" class=\"tbl\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" >".
	                 "<thead><tr><th rowspan=\"2\">No</th><th rowspan=\"2\">N a m a </th><th rowspan=\"2\">NIP</th><th colspan=\"2\">Pangkat</th>".
	                 "<th colspan=\"2\">Jabatan</th><th colspan=\"2\">Masa Kerja </th><th colspan=\"3\">Latihan Jabatan </th>".
	                 "<th colspan=\"3\">Pendidikan</th><th rowspan=\"2\">Tgl Lahir </th><th rowspan=\"2\">Catatan Mutasi Kepeg </th>".
	                 "<th rowspan=\"2\">Ket</th></tr><tr><th>Gol / Ruang </th><th>Tmt</th><th>Nama</th><th>Tmt</th>".
	                 "<th>Thn</th><th>Bln</th><th>Nama</th><th>Bln &amp; Thn </th><th>Jml Jam </th><th>Nama</th>".
	                 "<th>Thn Lulus </th><th>Tk. Ijazah </th></tr>".
            	     "</thead><tbody>";
			} else {
            	$s = '<table id="report" align="center" border="1" cellpadding="2" cellspacing="0"><tr>'.
                     '<th rowspan="2" class="num">No</th><th rowspan="2">N a m a </th><th rowspan="2">NIP</th>'.
                     '<th colspan="2">Pangkat</th><th colspan="2">Jabatan</th><th colspan="2">Masa Kerja </th>'.
                     '<th colspan="3">Latihan Jabatan </th><th colspan="3">Pendidikan</th><th rowspan="2" class="lebar3">Tgl Lahir </th>'.
                     '<th rowspan="2" class="lebar3">Catatan Mutasi Kepeg </th><th rowspan="2">Ket</th></tr>'.
                     '<tr><th class="lebar3">Gol / Ruang </th><th>Tmt</th><th>Nama</th><th class="lebar2">Tmt</th>'.
                     '<th class="lebar2">Thn</th><th class="lebar2">Bln</th><th>Nama</th><th class="lebar2">Bln &amp; Thn </th>'.
                     '<th class="lebar2">Jml Jam </th><th>Nama</th><th class="lebar2">Thn Lulus </th><th class="lebar2">Tk. Ijazah </th></tr>'.
            	     "<tr class=\"col_num\"><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td>".
                     "<td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td>".
                     "<td>15</td><td>16</td><td>17</td><td>18</td></tr>";
			}
            return $s;
		}
		
        function isi_daftar_urut_kepangkatan($print_mode=false, $instansi='') {
            $this->db->order_by('nama asc');
			if($instansi!='')
				$this->db->where('id_instansi',$instansi);
			else{
				echo "<script>alert('Pilih SKPD!');                          
                          window.history.back();  
					</script>";
				return false;
				}
            $q = $this->db->get('pegawai');

            $s = $print_mode?"":"[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
			
            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
            	if ($print_mode==false) {
	                if ($i % 2 == 1) {
	                    $kelas = "event2";
	                } else {
	                    $kelas = "event";
	                }
            	}

            	//Get pangkat terakhir
                $id_pangkat_terakhir = Datum_model::getLatestData('idspg_pengangkatan_pangkat', 'pengangkatan_pangkat', array('id_pegawai' => $row->idspg_pegawai));
                $pangkat_terakhir = Datum_model::getData('id_golongan', 'pengangkatan_pangkat', array('idspg_pengangkatan_pangkat' => $id_pangkat_terakhir));
                
                if (($pangkat_terakhir==false)) { continue; }
                
                $tmt_pangkat = Datum_model::getData('tmt', 'pengangkatan_pangkat', array('idspg_pengangkatan_pangkat' => $id_pangkat_terakhir));
                $this->db->select('nama, pangkat');
                $this->db->where(array('idspg_golongan' => $pangkat_terakhir));
				$query = $this->db->get('golongan');
				if ($query->num_rows() > 0) {
					$r = $query->row();
					$pangkat = $r->nama;//.' - '.$r->pangkat;
				} else {
					$pangkat = '-';
				}
				
				//Get jabatan terakhir
                $id_jabatan_terakhir = Datum_model::getLatestData('idspg_pengangkatan_jabatan', 'pengangkatan_jabatan', array('id_pegawai' => $row->idspg_pegawai));
				$jabatan_terakhir = Datum_model::getData('id_jabatan', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
                $tmt_jabatan = Datum_model::getData('tmt', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
				$jabatan = Datum_model::getData('nama', 'jabatan', array('idspg_jabatan' => $jabatan_terakhir));
                
				//Extract data masa kerja
				$masa_kerja = getMasaKerja($tmt_pangkat);
				$arr = explode(',', $masa_kerja);
				$masa_kerja_t = str_replace(' tahun', '', $arr[0]);
				$masa_kerja_b = str_replace(' bulan', '', $arr[1]);

				//Latihan jabatan/diklat struktural
                $id_diklat_terakhir = Datum_model::getLatestData('idspg_diklat', 'diklat', array('id_pegawai' => $row->idspg_pegawai, 'id_jenis_diklat' => TIPE_DIKLAT_STRUKTURAL));
                $id_jenis_diklat_struktural = Datum_model::getData('id_jenis_diklat_struktural', 'diklat', array('idspg_diklat' => $id_diklat_terakhir)); 
                $tgl_mulai = Datum_model::getData('tgl_mulai', 'diklat', array('idspg_diklat' => $id_diklat_terakhir));
                if ($tgl_mulai) {
	                $arr = explode(TGL_SEPARATOR, trimTime($tgl_mulai));
	                $bln_thn_jabatan = $arr[1].'/'.$arr[0];
                } else {
	                $bln_thn_jabatan = '';
                } 
                $total_jam = Datum_model::getData('total_jam', 'diklat', array('idspg_diklat' => $id_diklat_terakhir)); 
                $id_jenis_diklat = Datum_model::getData('id_kategori_diklat_struktural', 'jenis_diklat_struktural', array('idspg_jenis_diklat_struktural' => $id_jenis_diklat_struktural)); 
                $nama_diklat_struktural = Datum_model::getData('nama', 'jenis_diklat_struktural', array('idspg_jenis_diklat_struktural' => $id_jenis_diklat_struktural)); 
                $kategori_diklat = Datum_model::getData('nama', 'kategori_diklat_struktural', array('idspg_kategori_diklat_struktural' => $id_jenis_diklat)); 

                //Pendidikan terakhir
                $id_pend_terakhir = Datum_model::getLatestData('idspg_pendidikan', 'pendidikan', array('id_pegawai' => $row->idspg_pegawai));
                $this->db->select('nama, tahun_lulus, tgl_ijazah');
                $this->db->where(array('idspg_pendidikan' => $id_pend_terakhir));
                $q1 = $this->db->get('pendidikan');
                if ($q1->num_rows() > 0) {
                	$row1 = $q1->row();
                	$nama_pend = $row1->nama;
                	$tahun_lulus_pend = $row1->tahun_lulus;
                	$tk_ijazah = YMD2DMY(trimTime($row1->tgl_ijazah), TGL_SEPARATOR);
                } else {
                	$nama_pend = '-';
                	$tahun_lulus_pend = '-';
                	$tk_ijazah = '-';
                }

                if ($print_mode==false) {
	                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idspg_pegawai."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>";
                } else {
	                $s .= "<tr><td><div align=\"right\">$i.</div></td>";
                }
                $s .= "<td>".str_replace($order, $replace, $row->gelar_depan.' '.$row->nama.' '.$row->gelar_belakang)."</td>".
                      "<td>".$row->nip.' / <br />'.$row->nip_baru."</td>".
                      "<td><div align=center>".$pangkat."</div></td>".
                      "<td>".YMD2DMY(trimTime($tmt_pangkat), TGL_SEPARATOR)."</td>".
                	  "<td>".$jabatan."</td>".
                      "<td>".YMD2DMY(trimTime($tmt_jabatan), TGL_SEPARATOR)."</td>".
                      "<td><div align=center>".$masa_kerja_t."</div></td>".
                      "<td><div align=center>".$masa_kerja_b."</div></td>".
                	  "<td>".$kategori_diklat.' '.$nama_diklat_struktural."</td>".
                	  "<td>".$bln_thn_jabatan."</td>".
                	  "<td><div align=center>".$total_jam."</div></td>".
                      "<td>".$nama_pend."</td>".
                      "<td>".$tahun_lulus_pend."</td>".
                      "<td>".$tk_ijazah."</td>".
                      "<td>".YMD2DMY(trimTime($row->tgl_lahir), TGL_SEPARATOR)."</td>".
                      "<td>".'&nbsp;'."</td>".
                      "<td>".'&nbsp;'."</td></tr>";
                if ($print_mode==false) {
                	$s .= "'],";
                }

                $i++;
            }
            if ($print_mode==false) {
	            $s = substr($s, 0, -1);
	            $s .= '];';
            } else {
            	$s .= "</tbody></table>";
            }
            return $s;
        }

		

		function header_jumlah_jabatan_struktural($print_mode=false) {
			if ($print_mode==false) {
            	$s = "<table id=\"myTable\" class=\"tbl\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" >".
                     "<thead><tr><th rowspan=\"2\">No</th><th rowspan=\"2\">Unit Organisasi</th><th rowspan=\"2\">Jumlah</th><th colspan=\"8\">E s e l o n</th>".
                     "</tr><tr><!--<th>IA</th><th>IB</th><th>IIA</th>--><th>IIB</th><th>IIIA</th><th>IIIB</th><th>IVA</th><th>IVB</th></tr>".
            	     "</thead><tbody>";
			} else {
            	$s = '<table id="report" align="center" border="1" cellpadding="2" cellspacing="0">'.
            	     '<tr><th rowspan="2" class="num">No</th><th rowspan="2">Unit Organisasi  </th>'.
                     '<th rowspan="2" class="lebar3">Jumlah</th><th colspan="8">E s e l o n</th></tr><tr>'.
                     #'<th class="lebar2">IA</th><th class="lebar2">IB </th><th class="lebar2">IIA </th>'.
                     '<th class="lebar2">IIB</th><th class="lebar2">IIIA</th><th class="lebar2">IIIB </th>'.
                     '<th class="lebar2">IVA</th><th class="lebar2">IVB</th></tr>'.
            	     "<tr class=\"col_num\"><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td>".
                     "<td>7</td><td>8</td><td>9</td><td>10</td><td>11</td></tr>";
			}
            return $s;
		}
		
		
        function isi_jumlah_jabatan_struktural($print_mode=false) {
            $this->db->order_by('nama asc');
            $this->db->where(array('id_kabkota' => KABKOTA));
            $q = $this->db->get('instansi');

            $s = $print_mode?"":"[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
			
            $arr_tot_eselon = array(0, 0, 0, 0, 0, 0, 0, 0);
            $this->load->model('Datum_model');
            $grand_total = 0;
            foreach($q->result() as $row) {
            	$arr_jum_eselon = array(0, 0, 0, 0, 0, 0, 0, 0);
            	if ($print_mode==false) {
	                if ($i % 2 == 1) {
	                    $kelas = "event2";
	                } else {
	                    $kelas = "event";
	                }
            	}
            	
            	//Get pegawai yg termasuk unit ttt
            	$this->db->where(array('id_instansi' => $row->idspg_instansi));
            	$this->db->select('idspg_pegawai');
            	$q1 = $this->db->get('pegawai');
            	$total = 0;
            	foreach ($q1->result() as $peg) {
					//Get jabatan struktural terakhir u/ ngitung jumlah eselon
	                $id_jabatan_terakhir = Datum_model::getLatestData('idspg_pengangkatan_jabatan', 'pengangkatan_jabatan', array('id_pegawai' => $peg->idspg_pegawai));
					$jabatan_terakhir = Datum_model::getData('id_jabatan', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
					$jabatan = Datum_model::getData('tipe', 'jabatan', array('idspg_jabatan' => $jabatan_terakhir));
                	if ($jabatan==TIPE_JABATAN_STRUKTURAL) {
                		$eselon = Datum_model::getData('id_eselon', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
                		switch ($eselon) {
                			#case 1: $arr_jum_eselon[0]++; $arr_tot_eselon[0]++; break;
                			#case 2: $arr_jum_eselon[1]++; $arr_tot_eselon[1]++; break;
                			#case 3: $arr_jum_eselon[2]++; $arr_tot_eselon[2]++; break;
                			case 4: $arr_jum_eselon[3]++; $arr_tot_eselon[3]++; break;
                			case 5: $arr_jum_eselon[4]++; $arr_tot_eselon[4]++; break;
                			case 6: $arr_jum_eselon[5]++; $arr_tot_eselon[5]++; break;
                			case 7: $arr_jum_eselon[6]++; $arr_tot_eselon[6]++; break;
                			case 8: $arr_jum_eselon[7]++; $arr_tot_eselon[7]++; break;
                		}
                	}
            	} //foreach pegawai
            	$total = array_sum($arr_jum_eselon);
            	$grand_total += $total;
            	
                if ($print_mode==false) {
	                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idspg_instansi."\" ><td><div align=\"right\">$i.</div></td>";
                } else {
	                $s .= "<tr><td><div align=\"right\">$i.</div></td>";
                }
                $s .= "<td>".$row->nama."</td>".
                      "<td><div align=center>".$total."</div></td>".
                      #"<td><div align=center>".$arr_jum_eselon[0]."</div></td>".
                      #"<td><div align=center>".$arr_jum_eselon[1]."</div></td>".
                      #"<td><div align=center>".$arr_jum_eselon[2]."</div></td>".
                      "<td><div align=center>".$arr_jum_eselon[3]."</div></td>".
                      "<td><div align=center>".$arr_jum_eselon[4]."</div></td>".
                      "<td><div align=center>".$arr_jum_eselon[5]."</div></td>".
                      "<td><div align=center>".$arr_jum_eselon[6]."</div></td>".
                      "<td><div align=center>".$arr_jum_eselon[7]."</div></td></tr>";
                if ($print_mode==false) {
	                $s .= "'],";
                }

                $i++;
            }
            if ($print_mode==false) {
            	$s .= "['<tr class=\"$kelas\"><td colspan=2><div align=center>Jumlah</div></td>";
            } else {
            	$s .= "<tr><td colspan=2><div align=center>Jumlah</div></td>";
            }
            $s .= "<td><div align=center>".$grand_total."</div></td>".
                  #"<td><div align=center>".$arr_tot_eselon[0]."</div></td>".
                  #"<td><div align=center>".$arr_tot_eselon[1]."</div></td>".
                  #"<td><div align=center>".$arr_tot_eselon[2]."</div></td>".
                  "<td><div align=center>".$arr_tot_eselon[3]."</div></td>".
                  "<td><div align=center>".$arr_tot_eselon[4]."</div></td>".
                  "<td><div align=center>".$arr_tot_eselon[5]."</div></td>".
                  "<td><div align=center>".$arr_tot_eselon[6]."</div></td>".
                  "<td><div align=center>".$arr_tot_eselon[7]."</div></td>".
                  "</tr>";
            if ($print_mode==false) {
	            $s .= "']];";
            } else {
            	$s .= "</tbody></table>";
            }
            return $s;
        }
        
        
		function header_persentase_jabatan($print_mode=false) {
			if ($print_mode==false) {
            	$s = "<table id=\"myTable\" class=\"tbl\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" >".
                     '<thead><th rowspan="3">No</th><th rowspan="3">Unit Organisasi  </th>'.
            	     '<th rowspan="3" >Jumlah</th><th colspan="6">Jabatan</th></tr>'.
            	     '<tr><th colspan="2">Struktural</th><th colspan="2">Jafung AK </th><th colspan="2">'.
            	     'Jafung Umum </th></tr><tr><th >Jml</th><th >%</th>'.
            	     '<th >Jml</th><th >%</th><th >Jml</th>'.
            	     '<th>%</th></tr>'.
            	     "</thead><tbody>";
			} else {
            	$s = '<table id="report" align="center" border="1" cellpadding="2" cellspacing="0">'.
            	     '<tr><th rowspan="3" class="num">No</th><th rowspan="3">Unit Organisasi  </th>'.
            	     '<th rowspan="3" class="lebar3">Jumlah</th><th colspan="6">Jabatan</th></tr>'.
            	     '<tr><th colspan="2">Struktural</th><th colspan="2">Jafung AK </th><th colspan="2">'.
            	     'Jafung Umum </th></tr><tr><th class="lebar2">Jml</th><th class="lebar2">%</th>'.
            	     '<th class="lebar2">Jml</th><th class="lebar2">%</th><th class="lebar2">Jml</th>'.
            	     '<th class="lebar2">%</th></tr>'.
            	     '<tr class="col_num"><td>1</td><td>2</td><td>3</td><td colspan="2">4</td>'.
            	     '<td colspan="2">5</td><td colspan="2">6</td></tr>';
			}
            return $s;
		}

		
        function isi_persentase_jabatan($print_mode=false) {
            $this->db->order_by('nama asc');
            $this->db->where(array('id_kabkota' => KABKOTA));
            $q = $this->db->get('unit_kerja');

            $s = $print_mode?"":"[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
			
            $this->load->model('Datum_model');
            $arr_tot = array(0, 0, 0);
            $arr_tot_persen = array(0.0, 0.0, 0.0);
            $grand_total = 0;
            foreach($q->result() as $row) {
	            $arr_jum = array(0, 0, 0);
	            $arr_persen = array(0.0, 0.0, 0.0);
            	if ($print_mode==false) {
	                if ($i % 2 == 1) {
	                    $kelas = "event2";
	                } else {
	                    $kelas = "event";
	                }
            	}
            	
            	//Get pegawai yg termasuk unit ttt
            	$this->db->where(array('id_unit_kerja' => $row->idspg_unit_kerja));
            	$this->db->select('idspg_pegawai');
            	$q1 = $this->db->get('pegawai');
            	$total = 0;
            	foreach ($q1->result() as $peg) {
					//Get jabatan struktural terakhir u/ ngitung jumlahnya
	                $id_jabatan_terakhir = Datum_model::getLatestData('idspg_pengangkatan_jabatan', 'pengangkatan_jabatan', array('id_pegawai' => $peg->idspg_pegawai, 'id_golongan' => 0));
					$jabatan_terakhir = Datum_model::getData('id_jabatan', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
					$jabatan = Datum_model::getData('tipe', 'jabatan', array('idspg_jabatan' => $jabatan_terakhir));
                	if ($jabatan==TIPE_JABATAN_STRUKTURAL) {
                		$arr_jum[0]++; 
                		$arr_tot[0]++;
                	}
					//Get jabatan non-struktural terakhir u/ ngitung jumlahnya
	                $id_jabatan_terakhir = Datum_model::getLatestData('idspg_pengangkatan_jabatan', 'pengangkatan_jabatan', array('id_pegawai' => $peg->idspg_pegawai, 'id_golongan !=' => 0));
					$jabatan_terakhir = Datum_model::getData('id_jabatan', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
					$jabatan = Datum_model::getData('tipe', 'jabatan', array('idspg_jabatan' => $jabatan_terakhir));
                	if ($jabatan==TIPE_JABATAN_FUNGSIONAL_AK) {
                		$arr_jum[1]++;
                		$arr_tot[1]++;
                	} else if ($jabatan==TIPE_JABATAN_FUNGSIONAL_AK) {
                		$arr_jum[2]++;
                		$arr_tot[2]++;
                	}
            	} //foreach pegawai
            	$total = array_sum($arr_jum);
            	$grand_total += $total;
            	foreach ($arr_jum as $idx => $jum) {
            		if ($total!=0) {
            			$arr_persen[$idx] = $arr_jum[$idx] / $total * 100;
            		} else {
            			$arr_persen[$idx] = 0;
            		}
            	}
            	
                if ($print_mode==false) {
	                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idspg_unit_kerja."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>";
                } else {
	                $s .= "<tr><td><div align=\"right\">$i.</div></td>";
                }
                $s .= "<td>".$row->nama."</td>".
                      "<td><div align=center>".$total."</div></td>".
                      "<td><div align=center>".$arr_jum[0]."</div></td>".
                      "<td><div align=center>".sprintf('%.2f', $arr_persen[0])."</div></td>".
                      "<td><div align=center>".$arr_jum[1]."</div></td>".
                      "<td><div align=center>".sprintf('%.2f', $arr_persen[1])."</div></td>".
                      "<td><div align=center>".$arr_jum[2]."</div></td>".
                      "<td><div align=center>".sprintf('%.2f', $arr_persen[2])."</div></td></tr>";
                if ($print_mode==false) {
	                $s .= "'],";
                }

                $i++;
            }
            if ($print_mode==false) {
            	$s .= "['<tr class=\"$kelas\"><td colspan=2><div align=center>Jumlah</div></td>";
            } else {
            	$s .= "<tr><td colspan=2><div align=center>Jumlah</div></td>";
            }
            
            
            foreach ($arr_tot as $idx => $jum) {
            	$arr_tot_persen[$idx] = $arr_tot[$idx] / $grand_total * 100;
            }
            $s .= "<td><div align=center>".$grand_total."</div></td>".
                  "<td><div align=center>".$arr_tot[0]."</div></td>".
                  "<td><div align=center>".sprintf('%.2f', $arr_tot_persen[0])."</div></td>".
                  "<td><div align=center>".$arr_tot[1]."</div></td>".
                  "<td><div align=center>".sprintf('%.2f', $arr_tot_persen[1])."</div></td>".
                  "<td><div align=center>".$arr_tot[2]."</div></td>".
                  "<td><div align=center>".sprintf('%.2f', $arr_tot_persen[2])."</div></td></tr>";
            if ($print_mode==false) {
            	$s .= "']];";
            } else {
            	$s .= "</tbody></table>";
            }
            return $s;
        }

        
		function header_jabatan_struktural($print_mode=false) {
			if ($print_mode==false) {
            	$s = "<table id=\"myTable\" class=\"tbl\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" >".
            	     '<thead><tr><th width="20" rowspan="3">No</th><th width="126" rowspan="3">Nama Jabatan </th>'.
            	     '<th width="72" rowspan="3">Eselon</th><th width="94" rowspan="3">Unit Organisasi (SKPD) </th>'.
            	     '<th colspan="7">Pemangku Jabatan </th></tr><tr><th width="128" rowspan="2">Nama</th>'.
            	     '<th width="72" rowspan="2">NIP</th><th width="72" rowspan="2">Tempat &amp; Tgl. Lahir </th>'.
            	     '<th colspan="2">Pangkat</th><th width="72" rowspan="2">Diklatpim</th>'.
            	     '<th width="53" rowspan="2">Tmt Jabatan </th></tr><tr><th width="72">Gol / Ruang </th>'.
            	     '<th width="72">Tmt</th></tr>';
            	     "</thead><tbody>";
			} else {
            	$s = '<table id="report" align="center" border="1" cellpadding="2" cellspacing="0">'.
            	     '<tr><th width="20" rowspan="3" class="num">No</th><th width="126" rowspan="3">Nama Jabatan </th>'.
            	     '<th width="72" rowspan="3" class="lebar3">Eselon</th><th width="94" rowspan="3">Unit Organisasi (SKPD) </th>'.
            	     '<th colspan="7">Pemangku Jabatan </th></tr><tr><th width="128" rowspan="2">Nama</th>'.
            	     '<th width="72" rowspan="2" class="lebar3">NIP</th><th width="72" rowspan="2" class="lebar3">Tempat &amp; Tgl. Lahir </th>'.
            	     '<th colspan="2">Pangkat</th><th width="72" rowspan="2" class="lebar3">Diklatpim</th>'.
            	     '<th width="53" rowspan="2" class="lebar2">Tmt Jabatan </th></tr><tr><th width="72" class="lebar3">Gol / Ruang </th>'.
            	     '<th width="72" class="lebar3">Tmt</th></tr><tr class="col_num"><td>1</td><td>2</td><td>3</td><td>4</td>'.
            	     '<td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td></tr>';
			}
            return $s;
		}

		
        function isi_jabatan_struktural($print_mode=false) {
            $this->db->order_by('jabatan.nama asc');
            $this->db->where(array('jabatan.tipe' => TIPE_JABATAN_STRUKTURAL));
            $q = $this->db->get('jabatan');

            $s = $print_mode?"":"[";
			$i = 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
			
            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
            	if ($print_mode==false) {
	                if ($i % 2 == 1) {
	                    $kelas = "event2";
	                } else {
	                    $kelas = "event";
	                }
            	}
            	
            	//Get pegawai yg menjabat jabatan ini
				$eselon = $tmt_jabatan = $diklatpim = $tmt_pangkat = '';
            	$this->db->order_by('tmt desc');
            	$this->db->where(array('id_jabatan' => $row->idspg_jabatan));
            	$this->db->select('idspg_pengangkatan_jabatan');
            	$q1 = $this->db->get('pengangkatan_jabatan');
            	if ($q1->num_rows() > 0) {
            		$row1 = $q1->row();
            		$this->db->where(array('pengangkatan_jabatan.idspg_pengangkatan_jabatan' => $row1->idspg_pengangkatan_jabatan));
            		$this->db->join('pegawai', 'pegawai.idspg_pegawai=pengangkatan_jabatan.id_pegawai');
		            $this->db->join('eselon', 'eselon.idspg_eselon=pengangkatan_jabatan.id_eselon');
		            $this->db->join('unit_kerja', 'unit_kerja.idspg_unit_kerja=pegawai.id_unit_kerja');
            		$this->db->select('eselon.nama as nama_eselon, unit_kerja.nama as unit, pegawai.idspg_pegawai as id_pegawai');
		            $this->db->select('pegawai.nama as nama, pegawai.nip as nip, pegawai.tempat_lahir as tempat_lahir');
		            $this->db->select('pegawai.tgl_lahir as tgl_lahir, pegawai.gelar_depan as gelar_depan, pegawai.gelar_belakang as gelar_belakang');
		            $this->db->select('pengangkatan_jabatan.tmt as tmt_jabatan');
            		$q2 = $this->db->get('pengangkatan_jabatan');
	            	if ($q2->num_rows() > 0) {
	            		$row2 = $q2->row();
	            		$jabatan               = $row->nama;
	            		$tmt_jabatan           = $row2->tmt_jabatan;
	            		$eselon                = $row2->nama_eselon;
	            		$unit_kerja            = $row2->unit;
	            		$nama_pegawai          = $row2->nama;
	            		$nip                   = $row2->nip;
	            		$tempat_lahir          = $row2->tempat_lahir;
	            		$tgl_lahir             = YMD2DMY(trimTime($row2->tgl_lahir), TGL_SEPARATOR);

	            		$id_pangkat_terakhir   = Datum_model::getLatestData('idspg_pengangkatan_pangkat', 'pengangkatan_pangkat', array('id_pegawai' => $row2->id_pegawai));
		                $pangkat_terakhir      = Datum_model::getData('id_golongan', 'pengangkatan_pangkat', array('idspg_pengangkatan_pangkat' => $id_pangkat_terakhir));
		                $tmt_pangkat           = Datum_model::getData('tmt', 'pengangkatan_pangkat', array('idspg_pengangkatan_pangkat' => $id_pangkat_terakhir));
		                $this->db->select('nama, pangkat');
		                $this->db->where(array('idspg_golongan' => $pangkat_terakhir));
						$q0 = $this->db->get('golongan');
						if ($q0->num_rows() > 0) {
							$r = $q0->row();
							$pangkat = $r->nama;//.' - '.$r->pangkat;
						} else {
							$pangkat = '-';
						}
		                $id_diklatpim          = Datum_model::getLatestData('idspg_diklat', 'diklat', array('id_pegawai' => $row2->id_pegawai, 'id_jenis_diklat' => TIPE_DIKLAT_KEPEMIMPINAN));
		                $diklatpim             = Datum_model::getLatestData('nama', 'diklat', array('idspg_diklat' => $id_diklatpim));
	            	}
	            	
	                if ($print_mode==false) {
		                $s .= "['<tr class=\"$kelas\" id=\"row-".$row->idspg_jabatan."\" ><td><div align=\"right\">$i.</div></td>";
	                } else {
		                $s .= "<tr><td><div align=\"right\">$i.</div></td>";
	                }
	                $s .= "<td>".$jabatan."</td>".
	                      "<td><div align=center>".$eselon."</div></td>".
	                      "<td>".$unit_kerja."</td>".
	                      "<td>".$nama_pegawai."</td>".
	                      "<td>".$nip."</td>".
	                      "<td>".$tempat_lahir.', '.$tgl_lahir."</td>".
	                      "<td><div align=center>".$pangkat."</div></td>".
	                      "<td><div align=center>".YMD2DMY(trimTime($tmt_pangkat), TGL_SEPARATOR)."</div></td>".
	                      "<td><div align=center>".$diklatpim."</div></td>".
	                      "<td><div align=center>".YMD2DMY(trimTime($tmt_jabatan), TGL_SEPARATOR)."</div></td></tr>";
	                if ($print_mode==false) {
		                $s .= "'],";
	                }
	
	                $i++;
            	}
            }
            
            if ($print_mode==false) {
            	$s = substr($s, 0, -1)."];";
            } else {
            	$s .= "</tbody></table>";
            }
            return $s;
        }

		
	}
?>